package com.qtt.app.controller;

import com.qtt.app.common.ApiResponse;
import com.qtt.app.entity.AppStudyHistory;
import com.qtt.app.entity.AppViewsRecord;
import com.qtt.app.service.AppStudyHistoryService;
import com.qtt.app.service.AppViewsRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static com.qtt.app.utils.Converter.getAsInteger;

@Api(tags = "个人中心模块", description = "RecordController")   //tags是api名字，description是简介，value:已废用
@RestController
@RequestMapping("/personalCenter")
public class RecordController {
    @Autowired
    AppStudyHistoryService appStudyHistoryService;
    @Autowired
    AppViewsRecordService appViewsRecordService;

    @ApiOperation(value = "查询我所有的视频观看历史记录", notes = "")
    @GetMapping("/studyVideoHistory/{userId}")
    public ApiResponse<List<AppStudyHistory>> getMyStudyVideoHistory(@PathVariable("userId") Long userId){
        List<AppStudyHistory> entities = appStudyHistoryService.getMyStudyHistoryAll(userId);
        if (entities.size()!=0){
            return ApiResponse.<List<AppStudyHistory>>builder().code(200).message("查看成功").data(entities).build();
        }
        else{
            return ApiResponse.<List<AppStudyHistory>>builder().code(400).message("该用户目前没有观看记录数据").data(entities).build();
        }
    }

    @ApiOperation(value = "查询我所有的浏览记录", notes = "")
    @GetMapping("/viewsRecord/{userId}")
    public ApiResponse<List<AppViewsRecord>> getMyViewsRecordAll(@PathVariable("userId") Long userId){
        List<AppViewsRecord> entities = appViewsRecordService.getMyViewsRecordAll(userId);
        if (entities.size()!=0){
            return ApiResponse.<List<AppViewsRecord>>builder().code(200).message("查看成功").data(entities).build();
        }
        else{
            return ApiResponse.<List<AppViewsRecord>>builder().code(400).message("该用户目前没有浏览数据").data(entities).build();
        }
    }

    @ApiOperation(value = "点击课程，添加浏览记录(首页、课程页)", notes = "只有点击出售的课程需要调用该接口.返回该记录在浏览记录表的id")  //value是Get /Msg/list旁边的那个api简介；notes是点开的介绍，可用于详细说明api作用
    @PostMapping("/addViewCourseRecourd")
    public ApiResponse<Integer> addViewCourseRecourd(@ApiParam(value="请求参数解析：用户id、课程generalId、课程名、课程图片。注意：viewsRecordId、gmtCreate、gmtModified这几个参数不用传 删掉。",required = true) @RequestBody(required=true) AppViewsRecord requestData
    ){
        int viewCourseRecordId = appViewsRecordService.addViewCourseRecourd(requestData);
        Integer id = getAsInteger(viewCourseRecordId);
        if (viewCourseRecordId==0) {//注意列表如果 返回的是空，需要这么判断，空列表为[]
            return ApiResponse.<Integer>builder().code(400).message("添加记录失败").data(id).build();
        }
        return ApiResponse.<Integer>builder().code(200).message("添加记录成功").data(id).build();
    }
}
